﻿child_personality_gain = {
	trigger = {
		is_adult = no
		number_of_personality_traits < personality_trait_limit
	}
	random_events = {
		100 = child_personality.0001 # craven, lazy or arbitrary
		100 = child_personality.0002 # arrogant, compassionate or callous
		100 = child_personality.0003 # deceitful, honest or humble
		100 = child_personality.0004 # generous, diligent or patient
		100 = child_personality.0005 # wrathful, forgiving or fickle
		300 = child_personality.0006 # vengeful, chaste or stubborn - Weighted x3 because it can only be triggered as the 3rd and final personality event
		100 = child_personality.0007 # just, cynical or temperate
		300 = child_personality.0008 # greedy, gregarious or lustful - Weighted x3 because it can only be triggered as the 3rd and final personality event
		100 = child_personality.0009 # brave, calm or zealous
		100 = child_personality.0010 # ambitious, sadistic or paranoid
		100 = child_personality.0011 # content, fickle or trusting
		100 = child_personality.0012 # impatient, shy or gluttonous
		
		100 = child_personality.7000 # diligent, gregarious, temperate
		100 = child_personality.7010 # zealous, ambitious, sadistic
		100 = child_personality.7020 # shy, paranoid, craven
		100 = child_personality.7030 # lazy, gluttonous, compassionate
		600 = child_personality.7040 # lustful, chaste - Weighted x6 because it can only be triggered as the 3rd and final personality event
		100 = child_personality.7050 # just, greedy, callous
		100 = child_personality.7060 # humble, cynical, content
		100 = child_personality.7070 # vengeful, deceitful, calm
		100 = child_personality.7080 # generous, fickle, arrogant
		100 = child_personality.7090 # forgiving, trusting, patient
		100 = child_personality.7100 # honest, arbitrary, impatient
		100 = child_personality.7200 # brave, stubborn, wrathful
		10 = child_personality.7300 # eccentric, compassionate, callous
		10 = child_personality.7400 # eccentric, fickle, stubborn

		#In prison
		100 = child_personality.0021 ##Gain lustful
		100 = child_personality.0022 ##Gain chaste
		100 = child_personality.0023 ##Gain gluttonous
		100 = child_personality.0024 ##Gain temperate
		100 = child_personality.0025 ##Gain greedy
		50 = child_personality.0026 ##Gain generous
		100 = child_personality.0027 ##Gain lazy
		100 = child_personality.0028 ##Gain diligent
		100 = child_personality.0029 ##Gain wrathful
		100 = child_personality.0030 ##Gain calm
		100 = child_personality.0031 ##Gain patient
		100 = child_personality.0032 ##Gain impatient
		100 = child_personality.0033 ##Gain arrogant
		100 = child_personality.0034 ##Gain humble
		100 = child_personality.0035 ##Gain deceitful
		50 = child_personality.0036 ##Gain honest
		100 = child_personality.0037 ##Gain craven
		100 = child_personality.0038 ##Gain brave
		100 = child_personality.0039 ##Gain shy
		100 = child_personality.0040 ##Gain gregarious
		100 = child_personality.0041 ##Gain ambitious
		100 = child_personality.0042 ##Gain content
		100 = child_personality.0043 ##Gain arbitrary
		100 = child_personality.0044 ##Gain just
		100 = child_personality.0045 ##Gain cynical
		100 = child_personality.0046 ##Gain zealous
		100 = child_personality.0047 ##Gain paranoid
		50 = child_personality.0048 ##Gain trusting
		50 = child_personality.0049 ##Gain compassionate
		100 = child_personality.0050 ##Gain callous
		100 = child_personality.0051 ##Gain sadistic
		100 = child_personality.0052 ##Gain stubborn
		100 = child_personality.0053 ##Gain fickle
		100 = child_personality.0054 ##Gain vengeful
		50 = child_personality.0055 ##Gain forgiving
	}
}

childhood_ongoing = {
	trigger = {
		is_ruler = yes
		is_available_child = yes
	}
	random_events = {
		chance_to_happen = 50
		400 = 0
		800 = childhood.0001 #Child gets a crush
		100 = childhood.0003 #Child gets a friend
		100 = childhood.0005 #Child gets a bully
		100 = childhood.0007 #Child gets a victim
		100 = childhood.1000 #Your friendship can turn into a crush
		100 = childhood.1001 #You make your bully stop being such a bully
		100 = childhood.1002 #Parent is your educator, "supportive_parent" opinion_modifier
		100 = childhood.1004 #Guardian parent reduces stress
		100 = childhood.1005 #Guardian parent gives stress
		100 = childhood.1006 #Guardian parent (diligent) gives stress to lazy child
		100 = childhood.1007 #Bully gives you stress
		100 = childhood.1008 #To have a victim reduces stress
		100 = childhood.1009 #Your friend reduces your stress
		100 = childhood.1010 #Crush gives/reduces stress
		100 = childhood.1011 #Child might return an existing crush
		100 = childhood.1012 #Cruel guardian parent take child's toy away
		100 = childhood.1013 #You destroy your victims toy
		100 = childhood.1015 #child studies diplomacy with friend/crush
		100 = childhood.1016 #child studies martial with friend/crush
		100 = childhood.1017 #child studies stewardship with friend/crush
		100 = childhood.1018 #child studies intrigue with friend/crush
		100 = childhood.1019 #child studies learning with friend/crush
		50 = childhood.2001 #Get a pet cat!
		50 = childhood.2003 #Get a pet dog!
		100 = child_personality.0061 # Child may gain a propensity to drinking and lose their shyness by watching their guardian
		
		#BP1
		100 = childhood.7000 # Steal an adults clothes!
		5 = childhood.7100 # See another child get annihilated in a brutal but historically-appropriate fashion!
		100 = childhood.7200 # Try to learn the language of a Friend/crush
		100 = childhood.7300 # Beat up another child for a hook
		100 = bp1_yearly.5721 # Roast marshmallows over a fire with another child
		150 = childhood.7400 # Your bodyguard protects you from your bully
		100 = childhood.7500 # Your guardian tells you a story about one of their memories around the bonfire
		100 = bp1_yearly.9034 # You find a terrifying bog witch
		100 = bp1_yearly.9037 # You find a bog corpse
		100 = bp1_yearly.9041 # Your sibling is sad over the death of your parent
		40 = bp1_yearly.5001 # Your sibling tries to kill you

		#BP2
		300 = bp2_yearly.8100 # You develop feelings for another kid, is this a crush? 
		300 = bp2_yearly.5005 # A winsome foreign hostage
		300 = bp2_yearly.5009 # You promise?
	}
}

on_3rd_birthday = {
	trigger = { age = 3 }

	events = {
		child_personality.3000 #Randomizes a childhood personality
		birth.3033 #lose the trait sickly
	}
}

on_6th_birthday = {
	trigger = { age = 6 }

	events = {
		childhood_education.9001 #Assigns the education corresponding to their trait
	}
}

on_10th_birthday = {
	trigger = { age = 10 }

	events = {
		child_personality.4000 #Assigns a sexuality to the child
		bp1_house_feud.9003
		bp1_house_feud.9004
		shogunate_culture.0100	# Ainu tattoo
	}
}

on_15th_birthday = {
	trigger = { age = 15 }

	events = {
		witch.1001
		secret_faith.1001
		bp2_yearly.2019 # Imaginary Friend - Eccentric Trait Event
	}
}

on_16th_birthday = {
	trigger = { age = 16 }

	events = {
		childhood_education.9103		# To make sure that every child gets a education
		child_personality.4011			# To make sure everyone gets a sexuality
		childhood.0900					# victim transforms into opinion modifier on adulthood
		childhood.0910					# bully transforms into opinion modifier on adulthood
		childhood.0998					# crush transforms into opinion modifier on adulthood
		coming_of_age.0001				# Learning
		coming_of_age.0002				# Stewardship
		coming_of_age.0003				# Martial
		coming_of_age.0004				# Intrigue
		coming_of_age.0005				# Diplomacy
		delay = { days = 2 }
		childhood_education.0005		# To remove education focus
		childhood_education.0006		# To remove stray guardians
		childhood_education.0007		# To remove childhood traits and make sure any education trait is applied
		coming_of_age.2000				# Sage Accolade
		coming_of_age.2001				# Sage Accolade high bonus
		coming_of_age.3000				# House Knight Accolade
		coming_of_age.3011				# Junior Emperors become Co-Emperors
		court_events.3091				# Dad promised you a warhorse
	}

	effect = {
		shogunate_fictional_nickname_effect = yes

		if = {
			limit = {
				is_ai = yes
				# Shouldn't matter _but_, just in case, we double-check that you're actually in a suitable diarchy.
				has_diarchy_active_parameter = diarchy_type_is_entrenched_regency
			}
			trigger_event = {
				id = diarchy.0111
				months = { 6 24 }
			}
		}
		#Yurt Domicile effects
		if = {
			limit = {
				warden ?= {
					government_has_flag = government_is_nomadic
					domicile ?= {
						has_domicile_building_or_higher = lifestyle_xp_yurt_03
					}
				}
			}
			warden.domicile ?= {
				switch = {
					trigger = has_domicile_parameter
					nomad_yurt_better_children_education_outcomes_lvl_1 = {
						random = {
							chance = {
								value = warden.learning
								multiply = 0.5
							}
							root = {
								add_trait = lifestyle_mystic
							}
						}
					}
					nomad_yurt_better_children_education_outcomes_lvl_2 = {
						random = {
							chance = {
								value = warden.learning
							}
							root = {
								add_trait = lifestyle_mystic
							}
						}
					}
				}
			}
		}
		# If they had the Imaginary Friend story cycle, determine if we need to end the story or assign an adult persona to the imaginary friend 
		if = {
			limit = {
				any_owned_story = { story_type = story_cycle_imaginary_friend }
				NOT = { has_trait = eccentric }
			}
			random_owned_story = {
				limit = { story_type = story_cycle_imaginary_friend }
				end_story = yes
			}
			remove_character_modifier = imaginary_friend_modifier
		}
		else = {
			random_owned_story = {
				limit = { story_type = story_cycle_imaginary_friend }
				random_list = {
					50 = { # Give them a devilish imaginary friend in adulthood
						set_variable = {
							name = imaginary_friend_disposition
							value = flag:devil
						}
						modifier = {
							root = { has_trait = sadistic }
							add = 10
						}
						modifier = {
							root = { has_trait = vengeful }
							add = 10 
						}
						modifier = {
							root = { has_trait = deceitful }
							add = 10 
						}
						modifier = {
							root = { has_trait = callous }
							add = 10 
						}
						modifier = {
							root = { has_trait = paranoid }
							add = 10 
						}
					}
					50 = { # Give them an angelic imaginary friend in adulthood
						set_variable = {
							name = imaginary_friend_disposition
							value = flag:angel 
						}
						modifier = {
							root = { has_trait = compassionate }
							add = 10 
						}
						modifier = {
							root = { has_trait = forgiving }
							add = 10 
						}
						modifier = {
							root = { has_trait = generous }
							add = 10 
						}
						modifier = {
							root = { has_trait = calm }
							add = 10 
						}
						modifier = {
							root = { has_trait = content }
							add = 10 
						}
					}
				}
			}
		}
		# Hostage reaches adulthood
		if = {
			limit = { is_hostage = yes }
			save_scope_as = hostage
			warden ?= {
				set_variable = {
					name = hook_chance
					value = scope:hostage.bp2_hostage_loyalty_hook_chance_value
				}
				trigger_event = {
					id = bp2_hostage_system.0200
					days = 5
				}
			}
		}
		#Norman adventurer creation effect
		#And swords for hire adventurers
		if = {
			limit = {
				laamp_youth_cultural_tradition_trigger = yes
			}
			trigger_event = {
				id = ep3_laamp_decision_event.1300
				years = 2
			}
		}
	}
}

#To make sure everyone, including character from history, gets a sexuality
on_action_add_sexuality = {
	trigger = { age >= 10 }

	events = {
		delay = { days = { 14 30 } }
		child_personality.4000 #To make sure everyone gets a sexuality
	}
}

on_birthday_education_events = {
	trigger = {
		is_adult = no
		age >= childhood_education_start_age
	}

	effect = {
		save_scope_as = child
		court_owner ?= {
			save_scope_as = court_owner
			# Chance to gain extra skills if you have a Court Tutor teaching
			court_position:court_tutor_court_position ?= {
				if = {
					limit = { has_variable = court_tutor_teach_kids }
					save_scope_as = court_tutor_scope
					random = {
						chance = {
							value = 5
							add = {
								value = "aptitude(court_tutor_court_position)"
								multiply = 15
							}
						}
						if = {
							limit = {
								aptitude = {
									court_position = court_tutor_court_position
									value >= 3
								}
							}
							save_scope_value_as = {
								name = skill_boost
								value = {
									integer_range = {
										min = 1
										max = 2
									}
								}
							}
						}
						else = {
							save_scope_value_as = {
								name = skill_boost
								value = 1
							}
						}
						scope:court_tutor_scope = {
							random_list = {
								1 = {
									modifier = {
										add = 1
										diplomacy > decent_skill_rating
									}
									save_temporary_scope_as = diplomacy_tutor
								}
								1 = {
									modifier = {
										add = 1
										martial > decent_skill_rating
									}
									save_temporary_scope_as = martial_tutor
								}
								1 = {
									modifier = {
										add = 1
										stewardship > decent_skill_rating
									}
									save_temporary_scope_as = stewardship_tutor
								}
								1 = {
									modifier = {
										add = 1
										learning > decent_skill_rating
									}
									save_temporary_scope_as = learning_tutor
								}
							}
						}
						scope:court_owner ?= {
							send_interface_toast = {
								type = event_royal_court_good
								title = court_tutor_teach_kids_title
								left_icon = scope:child
								right_icon = scope:court_tutor_scope
								if = {
									limit = { this = scope:child }
									custom_tooltip = court_tutor_teach_kids_notification_desc_you
								}
								else = { custom_tooltip = court_tutor_teach_kids_notification_desc }
								scope:child = {
									switch = {
										trigger = exists
										scope:diplomacy_tutor = { add_diplomacy_skill = scope:skill_boost }
										scope:martial_tutor = { add_martial_skill = scope:skill_boost }
										scope:stewardship_tutor = { add_stewardship_skill = scope:skill_boost }
										scope:learning_tutor = { add_learning_skill = scope:skill_boost }
									}
								}
							}
						}
					}
				}
			}
		}
	}

	events = {
		court_maintenance.0003 #All children should acquire Personality Traits
	}
	
	on_actions = {
		childhood_education_boost
		childhood_education
	}
}

#To check if character needs education boost or not
childhood_education_boost = {
	trigger = {
		NOT = { has_character_flag = natural_education_progression }
		NOT = { age = 16 } # Already fired in 'on_16th_birthday' on_action
		has_education_focus_trigger = yes
	}
	events = {
		childhood_education.9103 # Education catchup
	}
}

#To gain education points
childhood_education = {
	trigger = {
		is_adult = no
		age >= childhood_education_start_age
	}

	events = {
		childhood_education.9002
		childhood_education.1001
		childhood_education.9004
		childhood_education.9010
	}
	on_actions = {
		childhood_education_patronage
	}
}

#Patronage-focused / scholarly spouse may adds points
childhood_education_patronage = {
	trigger = {
		exists = host.cp:councillor_spouse
		host.cp:councillor_spouse = { save_temporary_scope_as = councillor }
		spouse_learning_tier_1_trigger = yes
		has_education_focus_trigger = yes
		trigger_if = {
			limit = {
				has_variable = patronage_spouse_tutor
			}
			var:patronage_spouse_tutor = scope:councillor
		}
	}
	events = {
		childhood_education.9003
	}
}

on_ward_depart_for_guardian = {}

on_ward_depart_travel_planner_exit = {}

on_ward_arrive_at_guardian = {
	effect = {
		if = {
			limit = {
				# Ward still valid
				is_alive = yes
				is_adult = no
				is_playable_character = no
				num_of_relation_guardian = 0
				# Guardian still valid
				var:ward_travelling_to_guardian ?= {
					is_alive = yes
					num_of_relation_ward <= 1
				}
				OR = {
					scope:guardian = { is_playable_character = yes }
					exists = scope:guardian.host
					exists = scope:guardian.liege
				}
			}
			# Move to court
			if = {
				limit = {
					scope:guardian = { is_playable_character = yes }
					NOT = { host = scope:guardian }
				}
				visit_court_of = scope:guardian
			}
			else_if = {
				limit = {
					exists = scope:guardian.host
					NOT = { host = scope:guardian.host }
				}
				visit_court_of = scope:guardian.host
			}
			else_if = {
				limit = {
					exists = scope:guardian.liege
					NOT = { host = scope:guardian.liege }
				}
				visit_court_of = scope:guardian.liege
			}
			# Set relation
			set_relation_guardian = scope:guardian
			# Opinion
			guardian_add_opinion_effect = {
				GUARDIAN = scope:guardian
				WARD_LIEGE = scope:ward_liege
				WARD = scope:ward
			}
			# Convert culture setup
			scope:ward = {
				if = {
					limit = { has_character_flag = convert_culture }
					scope:guardian = {
						add_relation_flag = {
							relation = ward
							target = scope:ward
							flag = convert_culture
						}
					}
					remove_character_flag = convert_culture
				}
				# Convert faith setup
				if = {
					limit = { has_character_flag = convert_faith }
					scope:guardian = {
						add_relation_flag = {
							relation = ward
							target = scope:ward
							flag = convert_faith
						}
					}
					remove_character_flag = convert_faith
				}
			}
			# Fire events
			scope:guardian ?= { trigger_event = char_interaction.0083 }
			if = {
				limit = {
					NOT = { scope:guardian_liege ?= scope:guardian }
				}
				scope:guardian_liege = { trigger_event = char_interaction.0083 }
			}
			scope:ward_liege ?= { trigger_event = char_interaction.0084 }
		}
		else = { # Invalidate travel
			scope:guardian = {
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = ward_invalidated_during_travel_title
					left_icon = scope:ward
					right_icon = scope:guardian
					show_as_tooltip = {
						scope:ward = { remove_relation_guardian = scope:guardian }
					}
				}
			}
			scope:ward_liege ?= {
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = ward_invalidated_during_travel_title
					left_icon = scope:ward
					right_icon = scope:guardian
					scope:ward = {
						show_as_tooltip = { remove_relation_guardian = scope:guardian }
						current_travel_plan ?= {
							if = {
								limit = { can_cancel = yes }
								cancel_travel_plan = yes
							}
						}
						set_location_to_default = yes
					}
				}
			}
			debug_log = "Invalidated ward travel on arrival"
			debug_log_scopes = yes
		}
		# Clean up variables
		remove_variable = ward_travelling_to_guardian
		remove_variable = character_making_education_request
		scope:guardian ?= { remove_variable = guardian_waiting_for_ward }
	}
}

on_ward_depart_for_home = {}

on_ward_arrive_at_home = {
	effect = {
		# Move to court
		save_scope_as = ward
		var:guardian_educated ?= { save_scope_as = guardian }
		var:guardian_educated_realm ?= { save_scope_as = realm }
		if = {
			limit = {
				exists = scope:guardian 
				exists = liege
			}
			liege = {
				send_interface_message = {
					type = event_childhood_neutral
					title = ward_returned_home_title
					desc = ward_returned_home_desc_guardian
					left_icon = scope:ward
					right_icon = scope:guardian
					scope:ward = { return_to_court = yes }
				}
			}
		}
		else_if = {
			limit = { exists = liege }
			liege = {
				send_interface_message = {
					type = event_childhood_neutral
					title = ward_returned_home_title
					desc = ward_returned_home_desc
					left_icon = scope:ward
					scope:ward = { return_to_court = yes }
				}
			}
		}
		remove_variable = guardian_educated
		remove_variable = guardian_educated_realm
	}
}

on_guardian_depart_for_ward = {}

on_guardian_depart_travel_planner_exit = {}

on_guardian_arrive_at_ward = {
	effect = {
		if = {
			limit = {
				# Guardian still valid
				is_alive = yes
				num_of_relation_ward <= 1
				# Ward still valid
				var:guardian_travelling_to_ward ?= {
					is_alive = yes
					is_adult = no
					is_playable_character = yes
					num_of_relation_guardian = 0
				}
			}
			# Move to court
			visit_court_of = scope:ward
			# Set relation
			set_relation_ward = scope:ward
			# Opinion
			guardian_add_opinion_effect = {
				GUARDIAN = scope:guardian
				WARD_LIEGE = scope:ward_liege
				WARD = scope:ward
			}
			# Convert culture setup
			scope:ward = {
				if = {
					limit = { has_character_flag = convert_culture }
					scope:guardian = {
						add_relation_flag = {
							relation = ward
							target = scope:ward
							flag = convert_culture
						}
					}
					remove_character_flag = convert_culture
				}
				# Convert faith setup
				if = {
					limit = { has_character_flag = convert_faith }
					scope:guardian = {
						add_relation_flag = {
							relation = ward
							target = scope:ward
							flag = convert_faith
						}
					}
					remove_character_flag = convert_faith
				}
			}
			# Fire events
			scope:ward = { trigger_event = char_interaction.0082 }
			if = {
				limit = {
					NOT = { scope:ward_liege = scope:ward }
				}
				scope:ward_liege = { trigger_event = char_interaction.0082 }
			}
		}
		else = { # Invalidate travel
			scope:ward = {
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = guardian_invalidated_during_travel_title
					left_icon = scope:guardian
					show_as_tooltip = {
						scope:guardian = { remove_relation_ward = scope:ward }
					}
				}
			}
			scope:ward_liege ?= {
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = guardian_invalidated_during_travel_title
					left_icon = scope:ward
					right_icon = scope:guardian
					scope:guardian = {
						show_as_tooltip = { remove_relation_ward = scope:ward }
						current_travel_plan = {
							if = {
								limit = { can_cancel = yes }
								cancel_travel_plan = yes
							}
						}
						set_location_to_default = yes
					}
				}
			}
			debug_log = "Invalidated guardian travel on arrival"
			debug_log_scopes = yes
		}
		# Clean up variables
		var:guardian_travelling_to_ward ?= {
			remove_variable = character_making_education_request
			remove_variable = ward_waiting_for_guardian
		}
		remove_variable = guardian_travelling_to_ward
	}
}

on_guardian_depart_for_home = {}

on_guardian_arrive_at_home = {
	effect = {
		# Move to court
		save_scope_as = guardian
		var:ward_educated ?= { save_scope_as = ward }
		var:ward_educated_realm = { save_scope_as = realm }
		if = {
			limit = { exists = scope:ward }
			liege = {
				send_interface_message = {
					type = event_childhood_neutral
					title = guardian_returned_home_title
					desc = guardian_returned_home_desc_ward
					left_icon = scope:guardian
					right_icon = scope:ward
					scope:guardian = { return_to_court = yes }
				}
			}
		}
		else = {
			liege = {
				send_interface_message = {
					type = event_childhood_neutral
					title = guardian_returned_home_title
					desc = guardian_returned_home_desc
					left_icon = scope:guardian
					scope:guardian = { return_to_court = yes }
				}
			}
		}
		remove_variable = ward_educated
		remove_variable = ward_educated_realm
	}
}

on_adoptee_depart_for_adopter = {
	effect = {
		scope:adopter = {
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = adoptee_departs_title
				left_icon = scope:adoptee
				right_icon = scope:adopter
				custom_tooltip = adoptee_departs_tt
			}
		}
	}
}

on_adoptee_depart_travel_planner_exit = {}

on_adoptee_arrive_at_adopter = {
	effect = {
		if = {
			limit = { exists = scope:adopter }
			scope:adopter = {
				send_interface_message = {
					type = event_toast_effect_neutral
					title = adoptee_returned_home_title
					desc = adoptee_returned_home_desc
					left_icon = scope:adoptee
					add_courtier  = scope:adoptee
				}
			}
			remove_variable = adoptee_travelling_to_adopter
		}
	}
}
